package com.wolf.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wolf.common.enume.BizCodeEnum;
import com.wolf.gulimall.member.exception.PhoneException;
import com.wolf.gulimall.member.exception.UsernameException;
import com.wolf.gulimall.member.vo.MemberLoginVo;
import com.wolf.gulimall.member.vo.MemberRegisteredVo;
import com.wolf.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wolf.gulimall.member.entity.MemberEntity;
import com.wolf.gulimall.member.service.MemberService;
import com.wolf.common.utils.PageUtils;
import com.wolf.common.utils.R;


/**
 * 会员
 *
 * @author wolfmoon
 * @email wolfmoon@gmail.com
 * @date 2022-11-15 17:56:54
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;



    /**
     * 社交用户登录
     * @param socialUserVo 社交登录信息
     * @return 是否登录成功
     */
    @PostMapping("/oauth2/login")
    public R login(@RequestBody SocialUserVo socialUserVo) throws Exception {
        MemberEntity memberEntity= memberService.login(socialUserVo);
        if(memberEntity != null){
            return R.ok().setData(memberEntity);
        }else{
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
    }


    /**
     * 用户账号密码登录
     * @param memberLoginVo 登录信息
     * @return 是否登录成功
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){
         MemberEntity memberEntity= memberService.login(memberLoginVo);
         if(memberEntity != null){
             return R.ok().setData(memberEntity);
         }else{
             return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
         }
    }


    /**
     * 注册新用户
     * @param memberRegisteredVo 用户注册信息
     * @return 书册是否成功
     */
    @PostMapping("/registered")
    public R registered(@RequestBody MemberRegisteredVo memberRegisteredVo) {
        try {
            memberService.registered(memberRegisteredVo);
        } catch (PhoneException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        } catch (UsernameException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
